package com.adups.monitor.service.impl;


import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.adups.monitor.config.EMailConfig;
import com.adups.monitor.service.EmailService;
import com.adups.monitor.utils.StaticMethod;

/**
 * 邮件
 * 
 * @author 
 * @date 
 */
@Service("emailService")
public class EmailServiceImpl implements EmailService{
	@Autowired 
	private EMailConfig emailConfig;

	private final Logger logger = LoggerFactory.getLogger(EmailServiceImpl.class);

	public String sendMessage(String title,String message) {
		String toEmail = emailConfig.getToEmail();
		if(StringUtils.isBlank(toEmail) || !StaticMethod.chackEmailAdress(toEmail)){
			return "邮箱格式错误！"; 
		}
		logger.debug("send email to " + toEmail);
		Email email = new SimpleEmail();
		email.setHostName(emailConfig.getSmtp());
		email.setSmtpPort(Integer.valueOf(emailConfig.getPort()));
		email.setAuthenticator(new DefaultAuthenticator(emailConfig.getUser(),emailConfig.getPasswd()));
		email.setSSLOnConnect(true);
		try {
			email.setFrom(emailConfig.getFrom());
			email.setSubject(title);
			email.setMsg(message);
			email.addTo(toEmail);
			String ret = email.send();
			logger.info("send toEmail:" + toEmail + ",title:" + title + " ret" + ret);
			return "";
		} catch (EmailException e) {
			logger.error("send mail error,toEmail:" + toEmail + ",title:" + title + ",errormsg:" + e.getMessage());
		}
		return "发送邮件失败";
	}
	
	public String sendHtmlMessage(String title,String message) {
		String toEmailStr = emailConfig.getToEmail();
		
		String[] toEmails = toEmailStr.split(";");
		
		
		
		
		logger.debug("send email to " + toEmailStr);
		HtmlEmail email = new HtmlEmail();
		email.setCharset("utf-8");
		email.setHostName(emailConfig.getSmtp());
		email.setSmtpPort(Integer.valueOf(emailConfig.getPort()));
		email.setAuthenticator(new DefaultAuthenticator(emailConfig.getUser(),emailConfig.getPasswd()));
		email.setSSLOnConnect(true);
			
		try {
			email.setFrom(emailConfig.getFrom());
			email.setSubject(title);
			email.setHtmlMsg(message);	
			for(int i = 0 ; i < toEmails.length ; i++){
				String toEmail = toEmails[i];
				
				if(StringUtils.isBlank(toEmail) || !StaticMethod.chackEmailAdress(toEmail)){
					logger.error(toEmail+"邮箱格式错误！");
					continue;
				}
				email.addTo(toEmail);
			}
			
			String ret = email.send();
			logger.info("send toEmail:" + toEmailStr + ",title:" + title + " ret" + ret);
			return "";
		} catch (EmailException e) {
			logger.error("send mail error,toEmail:" + toEmailStr + ",title:" + title + ",errormsg:" + e.getMessage());
		}
		
			
		
		return "";
	}
	
	public  static void main(String[] arg){
		EmailServiceImpl kk = new EmailServiceImpl();
		//kk.sendMessage("dingwei626@qq.com", "测试邮件123", "你已经审核通过了3333222");
	}
}
